package com.fhz.day012;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;

import android.Manifest;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager supportFragmentManager = getSupportFragmentManager();
//        supportFragmentManager.beginTransaction().add().commit();

        requestPermissions(new String[]{Manifest.permission.ACCESS_NETWORK_STATE},100);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode  == 100){
            ConnectivityManager systemService = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = systemService.getActiveNetworkInfo();
            if(activeNetworkInfo!=null){
                boolean available = activeNetworkInfo.isConnected();
                if(available){
                    Toast.makeText(MainActivity.this, "有", Toast.LENGTH_SHORT).show();
                }
            }else
                Toast.makeText(MainActivity.this, "无", Toast.LENGTH_SHORT).show();
            }
        }
}